package so.shanku.essential.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.download.DownLoadQueue;
import net.tsz.afinal.download.DownloadService;
import net.tsz.afinal.sdcard.SdcardHelper;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.Utils;
import so.shanku.essential.view.WihteRoundCornersDialog;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Jaelyn on 16/2/25.
 * 设置
 */
public class SetActivity extends BaseUIActivity implements ServiceConnection {
    /**
     * content layout
     */
    @ViewInject(id = R.id.content_info_layout)
    private LinearLayout content_info_layout;
    /**
     * 账号管理
     */
    @ViewInject(id = R.id.accountManagerLayout, click = "accountManagerLayoutClick")
    private LinearLayout accountManagerLayout;
    /**
     * 安全管理
     */
    @ViewInject(id = R.id.securityLayout, click = "securityLayoutClick")
    private LinearLayout securityLayout;
    /**
     * 关于我们
     */
    @ViewInject(id = R.id.tr_my_about_us, click = "tr_my_about_usClick")
    private LinearLayout tr_my_about_us;
    /**
     * 帮助中心
     */
    @ViewInject(id = R.id.tr_my_help_center, click = "tr_my_help_centerClick")
    private LinearLayout tr_my_help_center;
    /**
     * 意见反馈
     */
    @ViewInject(id = R.id.tr_my_feedback, click = "tr_my_feedbackClick")
    private LinearLayout tr_my_feedback;
    /**
     * 版本检测
     */
    @ViewInject(id = R.id.tr_my_version_checking, click = "tr_my_version_checkingClick")
    private LinearLayout tr_my_version_checking;

    @ViewInject(id = R.id.my_the_current_version)
    private TextView my_the_current_version;
    /**
     * 联系我们
     */
    @ViewInject(id = R.id.tr_my_contact_us, click = "tr_my_contact_usClick")
    private LinearLayout tr_my_contact_us;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_layout);
        setCenter_title(R.string.systemSet);
        my_the_current_version.setText("V " + myApplication.getVersionName());
        Intent e = new Intent(this, DownloadService.class);
        this.startService(e);
        this.bindService(e, this, BIND_AUTO_CREATE);


    }

    public void accountManagerLayoutClick(View view) {

    }

    public void securityLayoutClick(View view) {
        jumpTo(UserChangeMainActivity.class);
    }

    public void tr_my_about_usClick(View view) {
        Intent intent = new Intent(mContext, UrlWebviewActivity.class);
        intent.putExtra(ExtraKeys.Key_Msg1, 7);
        jumpTo(intent, false);
    }

    public void tr_my_help_centerClick(View view) {
        Intent intent = new Intent(mContext, UrlWebviewActivity.class);
        intent.putExtra(ExtraKeys.Key_Msg1, 1);
        jumpTo(intent, false);
    }

    public void tr_my_feedbackClick(View view) {
        jumpTo(UserFeedBackActivity.class);
    }

    public void tr_my_version_checkingClick(View view) {
        upversion();
    }

    public void tr_my_contact_usClick(View view) {
        jumpTo(ContactUsActivity.class);
    }

    /**
     * 检测版本
     */
    private void upversion() {
        if (!SdcardHelper.ExistSDCard()) {
            toast.showToast(R.string.nosdcard);
        } else {
            loadingToast.show();
            GetDataQueue queue = new GetDataQueue();
            queue.setActionName(GetDataConfing.Action_selectAppVersionInfo);
            queue.setParams(new HashMap<String, String>());
            queue.setCallBack(callBack);
            queue.setWhat(GetDataConfing.What_selectAppVersionInfo);
            getDataUtil.getData(queue);
        }

    }

    private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

        @Override
        public void onLoaded(GetDataQueue entity) {
            loadingToast.dismiss();
            if (entity.isOk()) {
                if (ShowGetDataError.isCodeSuccess(mContext, entity.getInfo())) {
                    if (entity.what == GetDataConfing.What_selectAppVersionInfo) {

                        List<JsonMap<String, Object>> infoData = JsonParseHelper
                                .getJsonMap_List_JsonMap(entity.getInfo(), JsonKeys.Key_Info);
                        if (infoData.size() == 0) {
                            toast.showToast(R.string.app_version_newest);
                        } else {
                            JsonMap<String, Object> info = infoData.get(0);
                            if (info.getStringNoNull("VersionNo").equals(myApplication.getVersionName())
                                    || "".equals(info.getStringNoNull("VersionNo"))
                                    || "".equals(info.getStringNoNull("VersionPath"))) {
                                toast.showToast(R.string.app_version_newest);
                            } else {
                                showNewVersion(info);
                            }
                        }
                    }
                }
            } else {
                ShowGetDataError.showNetError(mContext);
            }

        }

    };
    /**
     * 提示用户有新版本
     */
    String apkDownLoadPath;
    WihteRoundCornersDialog builder;

    /**
     * 提示用户有新版本
     */
    private void showNewVersion(JsonMap<String, Object> info) {
        apkDownLoadPath = info.getStringNoNull("VersionPath");
        builder = new WihteRoundCornersDialog(mContext,
                R.style.ExitDialogStyle, 2, callBackdialog1);
        builder.setTitletext(R.string.app_version_select);
        builder.setMessagetext(info.getStringNoNull("Description"));
        builder.setButtonText(R.string.app_version_iknow, R.string.app_version_update);
        builder.show();
    }

    private WihteRoundCornersDialog.DialogCallBack callBackdialog1 = new WihteRoundCornersDialog.DialogCallBack() {

        @Override
        public void bttonclick(int index) {
            builder.dismiss();
            switch (index) {
                case 1:

                    break;
                case 2:
                    String appName = getString(R.string.app_name);
                    startDownload(apkDownLoadPath,appName+".apk",appName,1);

                    break;
                default:
                    break;
            }
        }

        @Override
        public void bttonclick(int index, int what) {

        }

    };



    private DownloadService.DownloadServiceBinder binder;

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        this.binder = (DownloadService.DownloadServiceBinder) service;
//        if(!this.isToStop) {
//            this.queue = this.binder.getService().addQueue(this.queue);
//        }
    }

    private DownLoadQueue queue;

    private void startDownload(String url, String fileName, String title, int id) {
        this.queue = new DownLoadQueue();
        this.queue.setId(id);
        this.queue.setIconResId(R.drawable.app_icon);
        this.queue.setCancel(false);
        this.queue.setDownLoadedPath(Constant.productPath + fileName);
        this.queue.setDownLoadUrl(url);
        this.queue.setName(title);
        this.queue.setOpenFileType("application/vnd.android.package-archive");
        this.queue.setDownLoadedAutoOpenFile(true, "application/vnd.android.package-archive");//自动打开app

        binder.getService().addQueue(this.queue);

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }

    @Override
    protected void onDestroy() {
        unbindService(this);
        super.onDestroy();
    }

}